﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Collections;
using Microsoft.Practices.Unity;

namespace iHouse.Common.Membership
{
    public class MembershipService : IMembershipService
    {
        private readonly MembershipProvider _provider;

        [InjectionConstructor]
        public MembershipService()
            : this(null)
        {
        }

        public MembershipService(MembershipProvider provider)
        {
            _provider = provider ?? System.Web.Security.Membership.Provider;
        }


        public int MinPasswordLength
        {
            get
            {
                return _provider.MinRequiredPasswordLength;
            }
        }

        public bool ValidateUser(string userName, string password)
        {
            return _provider.ValidateUser(userName, password);
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            _provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
            return currentUser.ChangePassword(oldPassword, newPassword);
        }

        public IEnumerable FindAll(int pageIndex, int pageSize)
        {
            int totalUserCount;
            return _provider.GetAllUsers(pageIndex, pageSize, out totalUserCount);
        }

        public MembershipUser GetUser(string userName)
        {
            return _provider.GetUser(userName, false);
        }

        public MembershipUser GetUser(object providerUserKey)
        {
            return _provider.GetUser(providerUserKey, false);
        }

        public bool DeleteUser(string userName,bool deleteAllRelatedData)
        {
            return _provider.DeleteUser(userName, deleteAllRelatedData);
        }

        public void UpdateUser(MembershipUser user)
        {
            _provider.UpdateUser(user);
        }

        public string ResetPassword(MembershipUser user)
        {
            return user.ResetPassword();
        }
    }
}
